

*set filepath. NOTE: you will need to update the filepath to run this file
cd "/Users/jeremyhorowitz/Documents/Book/Replication files/Chapter 5"


**************************************************************
********** Figure 5.1 ****************************************
**************************************************************

*PNU
use "appeals2007.dta", clear

keep if party=="PNU"

gen pos_sect_simp2 = pos_sect_simplified
replace pos_sect_simp2 = "Education" if pos_sect_simp2=="ed"
replace pos_sect_simp2 = "Infrastructure" if pos_sect_simp2=="infra"
replace pos_sect_simp2 = "Econ/jobs/poverty" if pos_sect_simp2=="econ/jobs/pov"
replace pos_sect_simp2 = "Gen Development" if pos_sect_simp2=="gen"
replace pos_sect_simp2 = "Gen Representation" if pos_sect_simp2=="genrep"
replace pos_sect_simp2 = "Health" if pos_sect_simp2=="health"
replace pos_sect_simp2 = "District creation" if pos_sect_simp2=="dist"
replace pos_sect_simp2 = "Women's employment" if pos_sect_simp2=="women/jobs"

replace pos_sect_simp2 = "Other" if pos_sect_simp2=="lands" | pos_sect_simp2=="freedom" | pos_sect_simp2=="women" | pos_sect_simp2=="title deeds" ///
| pos_sect_simp2=="squatters" | pos_sect_simp2=="security" | pos_sect_simp2=="taxes" | pos_sect_simp2=="sec (anti-majimbo)" | ///
pos_sect_simp2=="sec" | pos_sect_simp2=="police" | pos_sect_simp2=="parking" | pos_sect_simp2=="orphans" | pos_sect_simp2=="market" | ///
pos_sect_simp2=="dump" | pos_sect_simp2=="disabled" | pos_sect_simp2=="dev" | pos_sect_simp2=="appts" | pos_sect_simp2=="LATF/CDF" | pos_sect_simp2=="perf" | ///
pos_sect_simp2=="youth" | pos_sect_simp2=="youth/jobs" | pos_sect_simp2=="cdf"

sort pos_sect_simp2 uni_part
gen count=.
replace count=1 if pos_sect_simp2!=""
sort uni_part pos_sect_simp2
egen total_pos=total(count) if count!=.
by uni_part pos_sect_simp2: egen cat_total=total(count)
sort pos_sect_simp2
by pos_sect_simp2: egen cat_total_total=total(count)
gen cat_share_u = cat_total/total_pos if uni_part =="u"
gen cat_share_p = cat_total/total_pos if uni_part =="p"

graph bar cat_share_u cat_share_p, ///
over(pos_sect_simp2, label(angle(45)) sort(cat_total_total) reverse) stack  ///
scheme(s1mono) ytitle("Percent") ylabel(.1 "10" .2 "20" .3 "30", nogrid angle(0)) plotregion(lcolor(white)) title(PNU) bar(1, color(gs8) lcolor(black)) ///
bar(2, color(gs12) lcolor(black)) ///
legend(position(2) ring(0) bmargin(large) label(1 "Universal") label(2 "Targeted") col(1) order(2 1) symys(*.6) symxs(*.4) size(medium)) ///
graphregion(margin(b=32)) scale(1.2) name(g1, replace)

graph export "mygraph.pdf", replace


*ODM
use "appeals2007.dta", clear

keep if party=="ODM"

gen pos_sect_simp2 = pos_sect_simplified
replace pos_sect_simp2 = "Education" if pos_sect_simp2=="ed"
replace pos_sect_simp2 = "Infrastructure" if pos_sect_simp2=="infra" | pos_sect_simp2=="airport"
replace pos_sect_simp2 = "Econ/jobs/poverty" if pos_sect_simp2=="econ/jobs/pov"
replace pos_sect_simp2 = "Gen Representation" if pos_sect_simp2=="genrep"
replace pos_sect_simp2 = "Health" if pos_sect_simp2=="health"
replace pos_sect_simp2 = "Devolution (majimbo)" if pos_sect_simp2=="majimbo"
replace pos_sect_simp2 = "Women" if pos_sect_simp2=="women"
replace pos_sect_simp2 = "Corruption" if pos_sect_simp2=="corr"
replace pos_sect_simp2 = "Other" if pos_sect_simp2=="lands" | pos_sect_simp2=="freedom" | pos_sect_simp2=="title deeds" ///
| pos_sect_simp2=="squatters" | pos_sect_simp2=="security" | pos_sect_simp2=="taxes" | pos_sect_simp2=="sec (anti-majimbo)" | ///
pos_sect_simp2=="sec" | pos_sect_simp2=="police" | pos_sect_simp2=="parking" | pos_sect_simp2=="orphans" | pos_sect_simp2=="market" | ///
pos_sect_simp2=="dump" | pos_sect_simp2=="disabled" | pos_sect_simp2=="dev" | pos_sect_simp2=="appts" | pos_sect_simp2=="LATF/CDF" | pos_sect_simp2=="perf" | ///
pos_sect_simp2=="youth" | pos_sect_simp2=="district" | pos_sect_simp2=="hosing" | pos_sect_simp2=="other" | pos_sect_simp2=="tribalism" | pos_sect_simp2=="gen" | ///
pos_sect_simp2=="const" | pos_sect_simp2=="land"

sort pos_sect_simp2 uni_part
gen count=.
replace count=1 if pos_sect_simp2!=""
sort uni_part pos_sect_simp2
egen total_pos=total(count) if count!=.
by uni_part pos_sect_simp2: egen cat_total=total(count)
sort pos_sect_simp2
by pos_sect_simp2: egen cat_total_total=total(count)
gen cat_share_u = cat_total/total_pos if uni_part =="u"
gen cat_share_p = cat_total/total_pos if uni_part =="p"

graph bar cat_share_u cat_share_p, ///
over(pos_sect_simp2, label(angle(45)) sort(cat_total_total) reverse) stack  ///
scheme(s1mono) ytitle("Percent") ylabel(.1 "10" .2 "20" .3 "30", nogrid angle(0)) plotregion(lcolor(white)) title(ODM) bar(1, color(gs8) lcolor(black)) ///
bar(2, color(gs12) lcolor(black)) ///
legend(position(2) ring(0) bmargin(large) label(1 "Universal") label(2 "Targeted") col(1) order(2 1) symys(*.6) symxs(*.4) size(medium)) ///
graphregion(margin(b=-5)) scale(1.2) name(g2, replace)


graph combine g1 g2, rows(1) ycommon  xsize(9) scheme(s1mono) ysize(3.8) scale(*1.3)



**************************************************************
********** Table 5.2 ****************************************
**************************************************************

*PNU
use "appeals2007.dta", clear
keep if party=="PNU"
tab ethnic_majority, sort


*ODM
use "appeals2007.dta", clear
keep if party=="ODM"
tab ethnic_majority, sort



**************************************************************
********** Figure 5.2 ****************************************
**************************************************************

use "appeals2007.dta", clear
keep if pos_neg=="neg"
gen type=ifnegtype
replace type="Performance" if type=="perf"
replace type="Corruption" if type=="corr"
replace type="Favoritism / discrimination" if type=="fav"
replace type="Character" if type=="char"
replace type="Security risk" if type=="sec-risk" | type=="sec-risk (majimbo)"
drop if type==""

tab type if party=="PNU"
tab type if party=="ODM"
*manually create new .dta file with these data (negative appeals by party.dta)

*graph
use "negative appeals by party.dta", clear

graph bar pnu odm, over(type, label(angle(45)) sort(odm) reverse) ///
bar(1, color(gs8) lcolor(black)) bar(2, color(gs12) lcolor(black)) ///
scheme(s1mono) ytitle("Percent") ylabel(, nogrid angle(0))  plotregion(lcolor(white)) ///
legend(position(2) ring(0) label(1 "PNU") label(2 "ODM") row(1) order(2 1) symys(*.6) symxs(*.4) size(small)) ///
scale(1.2) 






